package org.jarraiders.musiclibrary.server;

import java.util.ArrayList;

import org.jarraiders.musiclibrary.client.AlbumsService;
import org.jarraiders.musiclibrary.domain.Album;
import org.jarraiders.musiclibrary.server.service.AlbumServiceDAO;
import org.jarraiders.musiclibrary.server.service.ConverterFabric;
import org.jarraiders.musiclibrary.shared.domain.AlbumDTO;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class AlbumsServiceImpl extends RemoteServiceServlet implements
		AlbumsService {
	
	private static AlbumServiceDAO service;
	static {
		service = new AlbumServiceDAO();
	}

	@Override
	public AlbumDTO addAlbum(AlbumDTO album) {
		Album albumP = ConverterFabric.getAlbumDAO(album);
		service.addNewEntity(albumP);
		return ConverterFabric.getAlbumDTO(service.getEntityById(albumP.getAlbumId()));
	}

	@Override
	public boolean deleteAlbum(String id) {
		//TODO: Check that place (maybe correct to change parametres)
		service.deleteEntity(service.getEntityById(id));
		return true;
	}

//	@Override
//	public ArrayList<AlbumDTO> deleteAlbums(ArrayList<String> ids) {
//		for (Integer id : ids) {
//			service.delete(id);
//		}
//		return getAlbumDetails();
//	}

	@Override
	public AlbumDTO getAlbum(String id) {

		return ConverterFabric.getAlbumDTO(service.getEntityById(id));
	}

	@Override
	public ArrayList<AlbumDTO> getAlbumDetails() {
		ArrayList<Album> list = (ArrayList<Album>) service.getAllEntities();
		ArrayList<AlbumDTO> listDTO = new ArrayList<AlbumDTO>();
		for (Album album : list) {
			listDTO.add(ConverterFabric.getAlbumDTO(album));
		}
		return listDTO;
	}

	@Override
	public AlbumDTO updateAlbum(AlbumDTO album) {
		Album albumP = ConverterFabric.getAlbumDAO(album);
		service.updateEntity(albumP);
		return ConverterFabric.getAlbumDTO(service.getEntityById(albumP.getAlbumId()));
	}

	@Override
	public ArrayList<AlbumDTO> searchAlbum(String searchText) {
		ArrayList <Album> list = (ArrayList<Album>) service.searchEntity(searchText); 
		ArrayList<AlbumDTO> listDTO = new ArrayList<AlbumDTO>();
		for (Album album : list) {
			listDTO.add(ConverterFabric.getAlbumDTO(album));
		}
		return listDTO;
	}
}
